package com.example.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.example.product.entity.*;
import com.example.product.service.*;
import com.example.product.vo.AttrVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.common.utils.PageUtils;
import com.example.common.utils.R;


/**
 * 商品属性
 *
 * @author lj
 * @email 980142391@qq.com
 * @date 2023-02-06 20:43:10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("product/attr")
public class AttrController {

    private final AttrService attrService;
    private final AttrGroupService attrGroupService;
    private final CategoryService categoryService;
    private final AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private ProductAttrValueService productAttrValueService;
//    /**
//     * 列表
//     */x
//    @GetMapping("/{attrType}/list/{catelogId}")
//    //@RequiresPermissions("product:attr:list")
//    public R baseList(@RequestParam Map<String, Object> params,@PathVariable("catelogId")Long catelogId,
//                      @PathVariable("attrType")String type){
////        PageUtils page = null;
////        if (catelogId == 0) {
////            page = attrService.queryPage(params);
//////            page.getList().stream().map(entity ->{x
//////                AttrRespVo attrRespVo = new AttrRespVo();
//////                BeanUtil.copyProperties(entity,attrRespVo);
//////                Long attrGroupId = attrAttrgroupRelationService.getOne(Wrappers.<AttrAttrgroupRelationEntity>lambdaQuery()
//////                                .eq(AttrAttrgroupRelationEntity::getAttrId,attrRespVo.getAttrId())).getAttrGroupId();
//////                if (attrGroupId != null){
//////                    attrRespVo.setCatelogName(attrGroupService.getById(attrGroupId).getAttrGroupName());
//////                }
//////                if (attrRespVo.getCatelogId() != null){
//////                    attrRespVo.setCatelogName(categoryService.getById(catelogId).getName());
//////                }
//////                return attrRespVo;
//////            }).collect(Collectors.toList());
//////            return R.ok().put("page", page);
////        }else {
////            String name = (String) params.get("key");
////            IPage<AttrEntity> attrEntityIPage = attrService.page(new Query<AttrEntity>().getPage(params), Wrappers.<AttrEntity>lambdaQuery()
////                    .eq(ObjectUtil.isNotEmpty(catelogId),AttrEntity::getCatelogId,catelogId)
////                    .like(ObjectUtil.isNotEmpty(name),AttrEntity::getAttrName,name));
////            page = new PageUtils(attrEntityIPage);
////
////        }
////        page.setList(page.getList().stream().map(entity ->{
////            AttrRespVo attrRespVo = new AttrRespVo();
////            BeanUtil.copyProperties(entity,attrRespVo);
////
////            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationService.getOne(Wrappers.<AttrAttrgroupRelationEntity>lambdaQuery()
////                    .eq(AttrAttrgroupRelationEntity::getAttrId,attrRespVo.getAttrId()));
////            if (attrAttrgroupRelationEntity != null){
////                attrRespVo.setCatelogName(attrGroupService.getById(attrAttrgroupRelationEntity.getAttrGroupId()).getAttrGroupName());
////            }
////            if (attrRespVo.getCatelogId() != null){
////                attrRespVo.setGroupName(categoryService.getById(attrRespVo.getCatelogId()).getName());
////            }
////            return attrRespVo;
////        }).collect(Collectors.toList()));
//        return R.ok().put("page", attrService.queryBaseAttrPage(params,catelogId,type));
//    }

    /**
     * 获取spu规格
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrlistforspu(@PathVariable("spuId") Long spuId) {

        List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrListforspu(spuId);

        return R.ok().put("data", entities);
    }

    /**
     * 查询规格参数信息
     *
     * @param params
     * @param catelogId
     * @param type
     * @return
     */

    //product/attr/sale/list/0?
    ///product/attr/base/list/{catelogId}
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType") String type) {

        PageUtils page = attrService.queryBaseAttrPage(params, catelogId, type);
        return R.ok().put("page", page);
    }

    ///product/attr/update/{spuId}
    @PostMapping("/update/{spuId}")
    //@RequiresPermissions("product:attr:update")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> entities) {

        productAttrValueService.updateSpuAttr(spuId, entities);

        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {
//		AttrEntity attr = attrService.getById(attrId);
//        AttrRespVo attrRespVo = new AttrRespVo();
//        BeanUtil.copyProperties(attr,attrRespVo);
//        AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationService.getOne(Wrappers.<AttrAttrgroupRelationEntity>lambdaQuery().eq(AttrAttrgroupRelationEntity::getAttrId,attr.getAttrId()));
//        if (attrAttrgroupRelationEntity != null){
//            attrRespVo.setCatelogName(attrGroupService.getById(attrAttrgroupRelationEntity.getAttrGroupId()).getAttrGroupName());
//        }
//        if (attrAttrgroupRelationEntity != null){
//            attrRespVo.setAttrGroupId(attrAttrgroupRelationEntity.getAttrGroupId());
//            attrRespVo.setGroupName(categoryService.getById(attrAttrgroupRelationEntity.getAttrGroupId()).getName());
//        }
        return R.ok().put("attr", attrService.getAttrInfo(attrId));
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attrVo) {
        attrService.save(attrVo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attrVo) {
//		attrService.updateById(attr);
        attrService.updateAttrById(attrVo);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
